package cn.jinhaze.utils;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.ssl.SSLContextBuilder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpsUtils {
    private static final String HTTP = "http";
    private static final String HTTPS = "https";
    private static SSLConnectionSocketFactory sslsf = null;
    private static PoolingHttpClientConnectionManager cm = null;
    private static SSLContextBuilder builder = null;
    static {
        try {
            builder = new SSLContextBuilder();
            // 全部信任 不做身份鉴定
            builder.loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            });
            sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register(HTTP, new PlainConnectionSocketFactory())
                    .register(HTTPS, sslsf)
                    .build();
            cm = new PoolingHttpClientConnectionManager(registry);
            cm.setMaxTotal(200);// max connection
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static CloseableHttpClient getHttpClient() throws Exception {
        return HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .setConnectionManager(cm)
                .setConnectionManagerShared(true)
                .build();
    }
    public static void setCommonHeaders(HttpPost httpPost) {
        httpPost.setHeader(new BasicHeader("accept-encoding", "gzip, deflate, br, zstd"));
        httpPost.setHeader(new BasicHeader("content-type", "application/json"));
        httpPost.setHeader(new BasicHeader("accept-language", "zh-CN,zh;q=0.9"));
        httpPost.setHeader(new BasicHeader("connection", "keep-alive"));
        httpPost.setHeader(new BasicHeader("host", "pfsc.agri.cn"));
        httpPost.setHeader(new BasicHeader("referer", "https://pfsc.agri.cn/"));
        httpPost.setHeader(new BasicHeader("sec-ch-ua", "\"Google Chrome\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\""));
        httpPost.setHeader(new BasicHeader("sec-ch-ua-mobile", "?0"));
        httpPost.setHeader(new BasicHeader("sec-ch-ua-platform", "\"Windows\""));
        httpPost.setHeader(new BasicHeader("sec-fetch-dest", "empty"));
        httpPost.setHeader(new BasicHeader("sec-fetch-mode", "cors"));
        httpPost.setHeader(new BasicHeader("sec-fetch-site", "same-origin"));
        httpPost.setHeader(new BasicHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"));
    }
}
